#!/bin/sh

# Author: Zhang Huangbin (michaelbibby <at> gmail.com)
# $DATE$

# chkconfig: 35 90 90
# description: run freshclam as daemon.

. /etc/rc.d/init.d/functions

OPTIONS='-d -c 2'
PID='/var/run/clamav/freshclam.pid'

start()
{
    if [ -e ${PID} ]; then
        echo -n "freshclam is already running: "
        echo -n "$(pidof freshclam)" && success
        echo
    else
        echo -n "Starting freshclam:"
        daemon freshclam ${OPTIONS} && success || failure
        echo
    fi
}

stop()
{
    if [ -e ${PID} ]; then
        echo -n "Stopping freshclam:"
        kill $(cat ${PID}) && success || failure
        echo
    else
        echo -n "freshclam is already stopped." && success
        echo
    fi
}

reload()
{
    if [ -e ${PID} ]; then
        echo -n "Reloading freshclam:"
        kill -HUP $(cat ${PID}) && success || failure
        echo
    else
        echo -n "freshclam isn't running." && failure
        echo
    fi
}

usage()
{
    echo "Usage: $0 [start|stop|restart|status|reload]"
}

case $1 in
    start) start ;;
    stop) stop ;;
    restart) stop; start ;;
    status) status freshclam ;;
    reload) reload ;;
    *) usage ;;
esac
